<?php

namespace Admin\Controller;
use Think\Controller;
class CourseController extends BaseController
{
    /*
     * 获取列表
     */
    public function index()
    {
        if (IS_POST) {

            $tea = M('course');
            if (!isset($_POST['page']) || empty($_POST['page'])) {
                $page = 1;
            } else {
                $page = $_POST['page'];
            }
            if (!isset($_POST['limit']) || empty($_POST['limit'])) {
                $limit = 10;
            } else {
                $limit = $_POST['limit'];
            }
            $condition['is_del'] = 0;
            if (isset($_POST['search'])) {
                $condition[] = [
                    'course_id' => array('like', "%" . $_POST['search'] . "%"),
                    'course_name' => array('like', "%" . $_POST['search'] . "%"),
                    'college_name' => array('like', "%" . $_POST['search'] . "%"),
                    '_logic' => 'OR',
                ];
            }
            //添加删除字段
            $count = $tea->where($condition)->count();
            $data = $tea->where($condition)->order('course_id desc')->page($page, $limit)->select() ?: [];
            
            return $this->successResponse('获取成功', $data, $count);
        } else {
            $this->display();
        }
    }

    /*
     * 删除
     */
    public function del()
    {
        if (IS_POST) {
            $course_id = $_POST['id'];
            $model = M('course');
            //PHP版本对数组形式
            $data = array(
                'is_del' => 1
            );
//            $rs = $model->where('course_id = '. $course_id)->setField($data);
            $rs = $model->where('course_id = ' . $course_id)->data($data)->save();
            if ($rs === false) {
                $this->ajaxReturn(false);
            } else {
                $this->ajaxReturn(true);
            }
        }
    }

    /*
     * 添加课程
     */
    public function add()
    {
        $course = M("course");
        $college = M("college");

        //自动收集表单
        if (empty($_POST))   //首先判断时侯为第一次打开页面
        {
            $where = [
                'is_del' => 0,
            ];
            $college_info = $college->where($where)->field('college_id, college_name')->select() ?: [];
            $data = [
                'college_info' => $college_info,
            ];
            $this->assign($data);
            $this->display();
        } else {
            $course->create(); //自动收集表单并且创建数据
            //自动递增教师编号
            $course_info = $course->limit(1)->order('course_id desc')->field('course_id')->find();
            $course_id = $course_info['course_id'];
            $college_info = $college->where('college_id =' . $_POST['college'])->field('college_name')->find();
            do {
                ++$course_id;
            } while ($course->where('course_id = ' . $course_id)->count('course_id'));

            $data = [
                'course_id' => $course_id,
                'course_name' => $_POST['course_name'],
                'credit' => $_POST['credit'],
                'college_name' => $college_info['college_name'] ?: '',
                'hours' => $_POST['hours'],
                'note' => $_POST['note'],
            ];

            $z = $course->add($data);
            if ($z) {
                $this->success("添加成功,返回页面", U("index"), 1);
                //success方法，3秒后自动跳转到add页面，默认是3秒
            } else {
                $this->error("添加失败！", U("index"), 1);
            }
        }
    }

    /*
     * 信息更新
     */
    public function update()
    {

        if (empty($_POST)) {
            $this->error('缺少输入值');
        }
        $course = M('course');
        $where = [
            'course_id' => $_POST['course_id'],
        ];
        $data = [
            //不能修改课程编号
            'course_name' => $_POST['course_name'],
            'credit' => $_POST['credit'],
            'hours' => $_POST['hours'],
            'college_name' => $_POST['college_name'],
            'note' => $_POST['note'],
            'is_del' => 0,
        ];
        $res = $course->where($where)->save($data);
        if ($res === false) {
            $this->error('修改失败');
        } else {
            $this->success('修改成功');
        }
    }

    /**
     * 获取课程详情页
     * @author llj <1063944289@qq.com>
     */
    public function courseDetail()
    {
        if ($_POST['id']) {
            $course = M('course');
            $college = D("college");

            $college_info = $college->where($where)->field('college_id, college_name')->select() ?: [];

            $col = [
                'college_info' => $college_info,
            ];

            $this->assign($col);
            $condition = array(
                'id' => $_POST['id'],
            );
            $data = $course->find($_POST['id']);
            if ($data) {
                $this->assign($data);
                $this->display('courseDetail');
            } else {
                $this->error('id不存在');
            }
        } else {
            $this->error('参数有误');
        }
    }



}

